import MobileIndex from './mobile/MobileIndex'

import PCIndex from './pc/PCIndex'
// 获取当前页面根字体大小
function adapt (designWidth, rem2px) {
  var d = window.document.createElement('div')
  d.style.width = '1rem'
  d.style.display = 'none'
  var head = window.document.getElementsByTagName('head')[0]
  head.appendChild(d)
  var defaultFontSize = parseFloat(window.getComputedStyle(d, null).getPropertyValue('width'))
  return defaultFontSize
}
// 根据当前字体大小，页面宽度，设计稿宽度，页面分成的列数等对字体大小进行百分比缩放以达到适配的目的
function setFontSize (doc, win, designWidth, rem2px) {
  let docEl = doc.documentElement
  let defaultFontSize = adapt(designWidth, rem2px)
  let resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize'
  let recalc = function () {
    let clientWidth = win.innerWidth || doc.documentElement.clientWidth || doc.body.clientWidth
    if (!clientWidth) return
    if (clientWidth < 750) {
      docEl.style.fontSize = clientWidth / designWidth * rem2px / defaultFontSize * 100 + '%'
    } else {
      docEl.style.fontSize = '625%'
    }
  }
  if (!doc.addEventListener) return
  win.addEventListener(resizeEvt, recalc, false)
  doc.addEventListener('DOMContentLoaded', recalc, false)
}
const components = {
  install (Vue) {
    Vue.component('mobile-index', MobileIndex)
    Vue.component('pc-index', PCIndex)
    Vue.prototype.judgeMobile = () => {
      let facility = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
      if (facility) {
        setFontSize(document, window, 750, 100)
      }
      return facility
    }
  }
}

export default components
